﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookStore.Solution.BLL;

namespace BookStore.Solution.Presentation
{
    public partial class ucPublisher : UserControl
    {
        PublisherManager publisherManager = new PublisherManager();
        public ucPublisher()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmPublisher frm = new FrmPublisher();
            frm.ShowDialog();
            if (!frm.Visible)
            {
                LoadData();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string pubID = "";
            if (grvListofPublisher.SelectedRows.Count > 0)
            {
                pubID = grvListofPublisher.CurrentRow.Cells["PublisherID"].Value.ToString();
                FrmPublisher frm = new FrmPublisher(pubID,false);
                frm.ShowDialog();
                if (!frm.Visible)
                {
                    LoadData();
                }
            }
            else
            {
                MessageBox.Show("Chọn ít nhất 1 nhà xuất bản để thay đổi thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnViewDetail_Click(object sender, EventArgs e)
        {
            string pubID = "";
            if (grvListofPublisher.SelectedRows.Count > 0)
            {
                pubID = grvListofPublisher.CurrentRow.Cells["PublisherID"].Value.ToString();
                FrmPublisher frm = new FrmPublisher(pubID, true);
                frm.ShowDialog();
            }
            else
            {
                MessageBox.Show("Chọn ít nhất 1 nhà xuất bản để xem chi tiết", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string pubID = "",result ="";
            if (grvListofPublisher.SelectedRows.Count > 0)
            {
                pubID = grvListofPublisher.CurrentRow.Cells["PublisherID"].Value.ToString();
                DialogResult dlr = MessageBox.Show("Bạn có chắc chắn xóa nhà xuất bản có mã: " + pubID + " ra khỏi hệ thống hay không?", "Xác nhận thông tin",
                                                     MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlr == DialogResult.Yes)
                {
                    publisherManager = new PublisherManager();
                    result = publisherManager.Delete(pubID);
                    if (result != "")
                    {
                        MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadData();
                    }
                }
            }
            else
            {
                MessageBox.Show("Chọn ít nhất 1 nhà xuất bản để xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

        private void btnSearchbyID_Click(object sender, EventArgs e)
        {
            FrmSearchbyPublisherID frm = new FrmSearchbyPublisherID();
            frm.ShowDialog();
            if (!frm.Visible && frm.result != null)
            {
                grvListofPublisher.AutoGenerateColumns = false;
                grvListofPublisher.DataSource = frm.result;
                for (int i = 0; i < grvListofPublisher.Columns.Count; i++)
                {
                    grvListofPublisher.Columns[i].DataPropertyName = grvListofPublisher.Columns[i].Name;
                }
            }
        }

        private void btnSearchbyName_Click(object sender, EventArgs e)
        {
            FrmSearchbyPublisherName frm = new FrmSearchbyPublisherName();
            frm.ShowDialog();
            if (!frm.Visible && frm.result != null)
            {
                grvListofPublisher.AutoGenerateColumns = false;
                grvListofPublisher.DataSource = frm.result;
                for (int i = 0; i < grvListofPublisher.Columns.Count; i++)
                {
                    grvListofPublisher.Columns[i].DataPropertyName = grvListofPublisher.Columns[i].Name;
                }
            }
        }

        private void ucPublisher_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        private void LoadData()
        {
            grvListofPublisher.AutoGenerateColumns = false;
            grvListofPublisher.DataSource = publisherManager.GetAll();
            for (int i = 0; i < grvListofPublisher.Columns.Count; i++)
            {
                grvListofPublisher.Columns[i].DataPropertyName = grvListofPublisher.Columns[i].Name;
            }
        }

        private void tsmiRefesh_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
